package com.zhangtai.utils;

import java.math.BigDecimal;

import static org.apache.commons.lang.StringUtils.isNumeric;
import static org.apache.commons.lang3.StringUtils.isBlank;

/**
 * @Author: ming
 * @Date: 2020/6/4 0004 下午 10:58
 */
public class NumToUpper {
    public static String arabiaToChinese(String number){
        if(isBlank(number)){
            return "零";
        }
        if(!isNumeric(number)){
            //是否是数字类型
            return "";
        }
        number = number.replaceAll(",","");
        number = number.replaceAll(" ","");
        if(Double.parseDouble(number) == 0){
            return "零";
        }
        BigDecimal decimal = new BigDecimal(number);
        number = decimal.toString();
        //分成两部分来处理
        String[] num = number.split("\\.");
        String head = num[0];
        if(head.length() > 10){
            System.out.println("数字超过十亿，无法计算");
            return "";
        }
        StringBuffer headNew = new StringBuffer();
        for(int i = head.length() - 1; i >= 0; i--){
            String si = String.valueOf(head.charAt(i));
            StringBuffer headNewString = new StringBuffer();
            //String ss = String.valueOf( num[0].charAt(i));
            switch (String.valueOf(head.charAt(i))){
                case "0":headNewString.append("零" + headNewString);break;
                case "1":headNewString.append("壹" + headNewString);break;
                case "2":headNewString.append("贰" + headNewString);break;
                case "3":headNewString.append("叁" + headNewString);break;
                case "4":headNewString.append("肆" + headNewString);break;
                case "5":headNewString.append("伍" + headNewString);break;
                case "6":headNewString.append("陆" + headNewString);break;
                case "7":headNewString.append("柒" + headNewString);break;
                case "8":headNewString.append("捌" + headNewString);break;
                case "9":headNewString.append("玖" + headNewString);break;
            }
            switch (num[0].length() - i - 1){
                case 0:headNewString.append("元");break;
                case 1:if(!"0".equals(si)) headNewString.append("拾");break;
                case 2:if(!"0".equals(si)) headNewString.append("佰");break;
                case 3:if(!"0".equals(si)) headNewString.append("仟");break;
                case 4:headNewString.append("万");break;
                case 5:if(!"0".equals(si)) headNewString.append("拾");break;
                case 6:if(!"0".equals(si)) headNewString.append("佰");break;
                case 7:if(!"0".equals(si)) headNewString.append("仟");break;
                case 8:headNewString.append("亿");break;
                case 9:headNewString.append("拾");break;
            }
            headNew = headNewString.append(headNew);
        }
        if(num.length > 1){
            if(num[1].length() > 2){
                System.out.println("小数点之后只能保留两位,系统将自动截断");
                num[1] = num[1].substring(0,2);
            }
            char[] tail = num[1].toCharArray();
            for (int i = 0; i < tail.length; i++) {
                StringBuffer tailNewString = new StringBuffer();
                switch (String.valueOf(tail[i])){
                    case "0":tailNewString.append("零" + tailNewString);break;
                    case "1":tailNewString.append("壹" + tailNewString);break;
                    case "2":tailNewString.append("贰" + tailNewString);break;
                    case "3":tailNewString.append("叁" + tailNewString);break;
                    case "4":tailNewString.append("肆" + tailNewString);break;
                    case "5":tailNewString.append("伍" + tailNewString);break;
                    case "6":tailNewString.append("陆" + tailNewString);break;
                    case "7":tailNewString.append("柒" + tailNewString);break;
                    case "8":tailNewString.append("捌" + tailNewString);break;
                    case "9":tailNewString.append("玖" + tailNewString);break;
                }
                if(i == 0) tailNewString.append("角");
                if(i == 1) tailNewString.append("分");
                headNew.append(tailNewString);
            }
        }
        String subject = headNew.toString();
        while (subject.contains("零零")) {
            subject = subject.replaceAll("零零", "零");
        }
        subject = subject.replaceAll("零亿","亿");
        subject = subject.replaceAll("亿万","亿");
        subject = subject.replaceAll("零万","万");
        subject = subject.replaceAll("零元","元");
        subject = subject.replaceAll("零角","");
        subject = subject.replaceAll("零分","");
        String c = String.valueOf(subject.charAt(subject.length() - 1));
        if("元".equals(c) || "角".equals(c)){
            subject = subject + "整";
        }
        return subject;
    }

}
